package com.glodon.paas.document.dao.mysql;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.glodon.paas.document.dao.ProjectResourceDao;
import com.glodon.paas.document.dao.domain.ProjectResourceEntity;
import com.glodon.paas.document.dao.mapper.ProjectResourceEntityMapper;
import com.glodon.paas.exception.PaasServerExceptionFactory;

public class ProjectResourceDaoMysqlImpl implements ProjectResourceDao {

    @Autowired
    private ProjectResourceEntityMapper mapper;

    @Override
    public String insert(ProjectResourceEntity entity) {
        if(mapper.insert(entity)>0){
            return entity.getId();
        }
        else{
            return null;
        }
    }

    @Override
    public ProjectResourceEntity get(String id) {
        throw PaasServerExceptionFactory.unImplementedException("unimplement get resource", null);
    }

    @Override
    public int delete(String id) {
        throw PaasServerExceptionFactory.unImplementedException("unimplement delete resource", null);
    }

    @Override
    public List<ProjectResourceEntity> selectByUser(String userId) {
        return mapper.selectByUser(userId);
    }

    @Override
    public List<ProjectResourceEntity> selectByUserFile(String userId, String fileId) {
        return mapper.selectByUserFile(userId, fileId);
    }

    @Override
    public ProjectResourceEntity selectByFileRole(String fileId, String roleId) {
        List<ProjectResourceEntity> rs = mapper.selectByFileRole(fileId, roleId);
        if(rs.size()>1){
            logger.warn("Too many ProjectResource, when fileId:"+fileId+", roleId:"+roleId);
            return rs.get(0);
        }
        else if(rs.size()==1){
                return rs.get(0);
        }
        else{
            return null;
        }
    }

    @Override
    public int update(ProjectResourceEntity entity) {
        return mapper.updateByPrimaryKey(entity);
    }

    @Override
    public List<ProjectResourceEntity> selectByFile(String fileId) {
        return mapper.selectByFile(fileId);
    }
    public void setProjectResourceEntityMapper(ProjectResourceEntityMapper mapper) {
        this.mapper = mapper;
    }
}
